/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DigestControllers;

import DigestModels.Database;
import DigestViews.DailySummaryView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Prophet, Tony,Sheamus
 */
public class DailySummaryController {
    ActionListener actionListener;
    Database database;
    DailySummaryView dailySummaryView;
    
    /**
     * This the default constructor
     * @param dailySummaryView represents the view for the daily summary.
     * @param database represents the database to be used.
     */
    public DailySummaryController(DailySummaryView dailySummaryView, Database database){
        this.dailySummaryView = dailySummaryView;
        this.database = database;
    }
    
    /**
     * This initializes the class and the view.
     */
    public void control(){
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == dailySummaryView.getDatePicker()){
                    getSummary();
                }
            }
        };
        dailySummaryView.getDatePicker().addActionListener(actionListener);
        dailySummaryView.getDatePicker().setDate(new Date());
        getSummary();
    }
    
    /**
     * This method retrieves the summary from the database and populates the summary view.
     */
    public void getSummary(){
        System.out.println("I come here");
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        String day = formater.format(dailySummaryView.getDatePicker().getDate());
        
        String summary = database.getDaySummay(day);
        if(summary.isEmpty())
            summary = "No Summary is not available for this day.";
        dailySummaryView.getSummary().setText(summary);
        
    }
    
}

